//
//  UICollectionView+Extension.swift
//  InPlace
//
//  Created by 松松 on 2018/10/16.
//  Copyright © 2018年 整物搭链. All rights reserved.
//

import Foundation
import UIKit

extension UICollectionView{

    //MARK:获取当前头视图的point
    public func sectionHederTop(_ section:Int) -> CGPoint {
        let indexPath = IndexPath(item: 0, section: section)
        let attr = self.layoutAttributesForSupplementaryElement(ofKind: UICollectionElementKindSectionHeader, at: indexPath)
        let attrY = attr?.frame.origin.y ?? 0
        let headerTop = CGPoint(x: 0, y: attrY - self.contentInset.top)
        return headerTop
    }

    //MARK:滑动到头视图
    public func scorlleToHeader(_ section : Int, _ animat:Bool){
        let headerTop = sectionHederTop(section)
        self.setContentOffset(headerTop, animated: animat)
    }

    //MARK:nib注册cell
    public func registCellNib<T:UICollectionViewCell>(_ aCell : T.Type){
        let cellName = String(describing: aCell)
        let nib = UINib(nibName: cellName, bundle: nil)
        register(nib, forCellWithReuseIdentifier: cellName)
    }

    //MARK:class注册cell
    public func registCellForClass<T:UICollectionViewCell>(_ aClass : T.Type){
        let cellName = String(describing: aClass)
        register(aClass, forCellWithReuseIdentifier: cellName)
    }

    //MARK:nib注册Header
    public func registHeaderNib<T:UICollectionReusableView>(_ aHeader : T.Type){
        let headerName = String(describing: aHeader)
        let nib = UINib(nibName: headerName, bundle: nil)
        register(nib, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerName)
    }

    //MARK:class注册Header
    public func registHeaderClass<T:UICollectionReusableView>(_ aClass : T.Type){
        let headerName = String(describing: aClass)
        register(aClass, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerName)
    }

    //MARK:nib注册Footer
    public func registFooterNib<T:UICollectionReusableView>(_ aFooter : T.Type){
        let footerName = String(describing: aFooter)
        let nib = UINib(nibName: footerName, bundle: nil)
        register(nib, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: footerName)
    }

    //MARK:class注册Footer
    public func registFooterClass<T:UICollectionReusableView>(_ aClass : T.Type){
        let footerName = String(describing: aClass)
        register(aClass, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: footerName)
    }

    public func dequeueReusableCell<T:UICollectionViewCell>(aCell : T.Type , indexPath : IndexPath)-> T{
        let cellName = String(describing: aCell)
        guard let cell = dequeueReusableCell(withReuseIdentifier: cellName, for: indexPath) as? T else{
            fatalError("\(cellName)没有注册")
        }
        return cell
    }

    public func dequeueReusableHeader<T:UICollectionReusableView>( aClass : T.Type , kind:String , indexPath : IndexPath) -> T{
        let headerName = String(describing: aClass)
        guard let header = dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerName, for: indexPath) as? T else{
            fatalError("\(headerName)没有注册")
        }
        return header
    }

}
